<?php
/**
 * This class represents 'medicine_usage_receipt' table.
 *
 * @package dept_medicine.core.ordo 
 * @author pktquynh
 * @version $Id:$
 * @copyright 2009 LHV Company
 */
class MedicineUsageReceipt extends ORDataObject {
    var $medicine_usage_receipt_id = '';
    var $department_id = '';
    var $name = '';
    var $modified = '';
    
    var $_table         = 'medicine_usage_receipt';
    var $_internalName  = 'MedicineUsageReceipt';
    var $_key           = 'medicine_usage_receipt_id';
    
    public function MedicineUsageReceipt($id = 0) {
        //call the parent constructor so we have a _db to work with
        parent::ORDataObject();     
    }
    
    /**
     * List medicine usage receipt of a department in some period.
     * @param $deptId The department id
     * @param $month month of period
     * @param $year year of period
     * @return array of medicine usage receipt 
     */
    public function listMedicineReceiptInPeriod($deptId, $month, $year) {
        $ds = new Datasource_sql();
        $sql = array (
            'cols'    => "medicine_usage_receipt_id as id, name",
            'from'    => $this->_table,
            'where'   => "department_id = '" . $deptId . "'"
                       . " AND MONTH(modified) = '" . $month . "'"
                       . " AND YEAR(modified) = '" . $year . "'");
        $ds->setup($this->_db, $sql);
        $result = $ds->toArray();
        return $result;
    }
    
    /**
     * Get information of a medicine usage receipt, including detail of the receipt such as medicine type,
     * medicine name, used number, remaining number ...
     * @param $medReceiptId The id of medicine usage receipt 
     * @return array contains information of medicine usage receipt
     */
    public function getMedUsageReceipt($medReceiptId) {
    	$fieldName =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
    	$fieldQuantityTitle =   L10nHelper::changeField(L10nHelper::getLocale(), 'quantity_title');
        
        $ds = new Datasource_sql();
        $sql = array (
            'cols'    => "murd.medicine_usage_receipt_detail_id as id, 
                        et.$fieldName as medicine_type, m.name as medicine_name,
                        m.medicine_id, mdu.$fieldQuantityTitle as quantity_title, murd.used_num, murd.remain_num, murd.requirement_num",
            'from'    => $this->_table . " mur"
                         . " INNER JOIN medicine_usage_receipt_detail murd ON mur.medicine_usage_receipt_id = mur.medicine_usage_receipt_id"
                         . " LEFT JOIN medicine m ON murd.medicine_id = m.medicine_id "
                         . " LEFT JOIN equipment_type et ON m.functional_type = et.equipment_type_id"
                         . " LEFT JOIN medicine_default_usage mdu ON m.default_usage_id = mdu.id",                         
            'where'   => "mur.medicine_usage_receipt_id = '" . $medReceiptId . "'",
            'orderby' => "medicine_type, medicine_name");
                         
        $ds->setup($this->_db, $sql);
        $result = $ds->toArray();
        return $result;
    }
   
}
?>